/**
 * 显示计划列列表，列入计划
 */
var projectPlan = {
	get : function() {
		var projectId = $("#projectId").val();
		$.ajax({
			dataType : "json",
			type : "post",
			data : {
				"projectId" : projectId
			},
			url : "/app/plan!listPlan",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					projectPlan.show(data.msg);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	},
	show : function(data) {
		var content = "<tr><th>暂无数据</tr></th>";
		if (null != data && 0 < data.length) {
			content = "<tr><th width='320' align='center'>计划名称</th><th width='80' align='center'>操作</th></tr>";
			for ( var int = 0; int < data.length; int++) {
				var v = data[int];
				var id = v.id;
				var name = v.name;
				content += "<tr><td width='320' align='center'>" + name
						+ "</td><td width='80' align='center'><a href='javascript:addPlanItem(" + id
						+ ")'>列入</a></td></tr>";
			}
			content += "</table></div></div>";
		}
		$("#content").html(content);
	}
};
function addPlanItem(planId) {
	art.dialog.confirm('确认列入？', function() {
		var projectId = $("#projectId").val();
		$.ajax({
			dataType : "json",
			type : "post",
			data : {
				"planId" : planId,
				"projectId" : projectId
			},
			url : "/app/plan!addPlanItem",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					tipsAlert(data.msg);
					projectPlan.get();
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	});
}

/**
 * 显示项目已列入的计划
 */
var projectPlanItem = {
	get : function() {
		var projectId = $("#projectId").val();
		$.ajax({
			dataType : "json",
			type : "post",
			data : {
				"projectId" : projectId
			},
			url : "/app/plan!listProjectPlanItem",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					projectPlanItem.show(data.msg);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	},
	show : function(data) {
		var content = "<tr><th>暂无数据</tr></th>";
		if (null != data && 0 < data.length) {
			content = "<tr><th width='320' align='center'>计划名称</th><th width='80' align='center'>操作</th></tr>";
			for ( var int = 0; int < data.length; int++) {
				var v = data[int];
				var id = v.id;
				var name = v.projectPlan.name;
				content += "<tr><td width='320' align='center'>" + name
						+ "</td><td width='80' align='center'><a href='javascript:deletePlanItem(" + id
						+ ")'>取消</a></td></tr>";
			}
			content += "</table></div></div>";
		}
		$("#content").html(content);
	}
};
/**
 * 取消计划
 * 
 * @param id
 */
function deletePlanItem(id) {
	art.dialog.confirm('确认取消？', function() {
		$.ajax({
			dataType : "json",
			type : "post",
			data : {
				"planItemId" : id
			},
			url : "/app/plan!deleteProjectPlanItem",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					projectPlanItem.get();
					tipsAlert(data.msg);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	});
}
$("input:radio[name='type']").change(function() {
	var v = $(this).val();
	switch (Number(v)) {
	case 1:
		projectPlan.get();
		break;
	case 2:
		projectPlanItem.get();
		break;
	}
});